#include "StdAfx.h"
#include "WMPPlusPublic.h"
#include "..\WMP\Common.h"
#include "WMPUtils.h"
#include "WMPPlusHookData.h"
#include "WMPPlus.h"
#include <Commctrl.h>

#pragma unmanaged

void WMPPlusPublic::Initialize() {
	WMPPlusBase::Initialize();
	setHookFolder(false);
	setMediaFolder(NULL);
	setInTitleDraw(false);
}

bool Kernel32Hooked() {
	return ((Core->Hook->MajorVersion<<16) | Core->Hook->MinorVersion) >= 0x10001 && 
		(Core->Hook->Libraries & IMPORT_LIBRARY_KERNEL32);
}

bool WMPPlusPublic::ResolveCurrentFolder(HWND hWnd, bool resolve, int &selected, LPCTSTR &medias) {
	medias = NULL;
	if (!Kernel32Hooked() || (selected = ListView_GetSelectedCount(hWnd)) == 0u)
		return false;
	setHookFolder(true);
	setResolveFolder(resolve);
	try {
		SendMessage(hWnd, WM_COMMAND, 7243, NULL);
	} finally (
		setHookFolder(false);
	)
	medias = Plugin->getMediaFolderList();
	return true;
}

void WMPPlusPublic::SetWMPTitle(LPCTSTR title) {
	if (getWMPlayerWindow()) {
		SetWindowText(getWMPlayerWindow(), title);
	}
	if (IsWindow(getWMPLibraryWindow())) {
		SetWindowText(getWMPLibraryWindow(), title);
	}
}

void WMPPlusPublic::UpdateTitle(LPCTSTR title) {
	RECT rect;
	RECT client;

	HWND hWnd = getWMPlayerWindow();
	bool hasbar = getWMPlayerVersion() <= 11 && !IsBarVisible(hWnd);
	if (hasbar)
		setInTitleDraw(true);
	try {
		SetWMPTitle(title);
		if (hasbar) {
			GetWindowRect(hWnd, &rect);
			GetClientRect(hWnd, &client);
			if (rect.right - rect.left == client.right - client.left &&
				rect.bottom - rect.top == client.bottom - client.top) {
				client.right -= client.left;
				client.bottom = 100;
				client.left = 0;
				client.top = 0;
				RedrawWindow(hWnd, &client, NULL, RDW_FRAME | RDW_ERASENOW | RDW_INVALIDATE);
				SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
			}
		}
	} finally (
		if (hasbar)
			setInTitleDraw(false);
	)
}

